package com.dev1.pack02service.impl;
import com.dev1.pack01web.bean.Province;
import com.dev1.pack02service.ProvinceService;
import com.dev1.pack03dao.ProvinceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class ProvinceServiceImp implements ProvinceService {
    @Autowired
    private ProvinceDao provinceDao = null;

    @Override
    public void addNums(String name, int i) {
//        Optional<Province> one = provinceDao.findById(name);
        if( provinceDao.existsById(name) ){

            Province province = provinceDao.getOne(name);
            province.setNums(province.getNums()+1);
            provinceDao.save(province); //update

        }else {

            Province province = new Province(name,1);
            provinceDao.save(province); //insert
        }
    }

    @Override

    public List<Province> findAllProvinces() {
        List<Province> all = provinceDao.findAll();
        System.out.println("-ProvinceServiceImp--findAllProvinces--"+all);
        return  all;
    }
}